package com.lib.module_base.utils

import android.view.View
import java.util.concurrent.TimeUnit
import io.reactivex.Observable


/**
 * @Author andy
 * @Date 2024/5/28 14:22
 */

/**
 * 给view设置'防抖动'点击事件
 * 连续点击在 interval 毫秒内只有1次生效
 */
fun View.setOnClickCallback(interval: Long = 1000L, callback: (View) -> Unit) {
    if (!isClickable) isClickable = true
    if (!isFocusable) isFocusable = true
    Observable.create<View> {
        setOnClickListener(it::onNext)
    }
        .throttleFirst(interval, TimeUnit.MILLISECONDS)
        .doOnNext(callback)
        .subscribe()
}